package com.selenium.dinner;

import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;

import com.google.common.base.Function;
import com.thoughtworks.selenium.DefaultSelenium;

public abstract class Abstract {
	private static final String WAIT_FOR_ELEMENT_VISIBLE = "Wait For Element Visible";
	private static final String TIMEOUT = "timeout";
	
	/* Gallery */
	protected static final String Previous = "//span[@class='als-prev']";
	protected static final String Next = "//span[@class='als-next']";
	protected static final String IMG_1 = "//div[@id='als-item_0_0']/img";
	protected static final String IMAGE_PREVIEW = "//div[@class='dinner-gallery-image-preview']";
	protected static final String IMG_7 = "//div[@class='als-wrapper']/div[4]/img";
	
	/* Previous Menu*/
	protected static final String Right_Arrows = "//span[contains(text(),'Next')]";
	protected static final String TEXT_25 = "//a[contains(text(),'25')]";
	protected static final String January = "//span[contains(text(), 'January')]";
	protected static final String April= "//span[contains(text(), 'April')]";
	protected static final String Left_Arrows = "//span[@class='ui-icon ui-icon-circle-triangle-w']";
	protected static final String BUTTON_LABEL_PREVIOUSMENU = "//div[@class='dinner-button-label-previousmenu']";
	protected static final String DATEPICKER_TRIGGER = "//img[contains(@class, 'ui-datepicker-trigger')]";
	protected static final String VIEW_COMPLAIN = "//div[@class='dinner-previous-admin-label-view-complain']";
	protected static final String BACK = "//div[contains(text(),'Back')]";
	protected static final String Details_last = "//div[@class='jspPane']/div[last()]/div[2]/div/div/div[position()=1]/span[last()]";
	protected static final String Details = "//div[@class='jspPane']/div[position()=1]/div[2]/div/div/div[position()=1]/span[last()]";
	protected static final String LAST_ARROW_DOWN = "//a[@class='jspArrow jspArrowDown jspDisabled jspActive']";
	protected static final String ARROW_DOWN = "//a[@class='jspArrow jspArrowDown']";
	
	/* Todays Menu */
	protected int attempt;
	protected static final String IMAGE_3 = "//img[@class='dinner-photo-todays-label3']";
	protected static final String IMAGE_2 = "//img[@class='dinner-photo-todays-label2']";
	protected static final String IMAGE_1 = "//img[@class='dinner-photo-todays-label1']";
	protected static final String SLIDER_USER_ToADMIN = "//div[contains(@class, 'dinner-slider-user')]";
	protected static final String BUTTON_YES = "//div[@class = 'dinner-main-content-masking']//div[@class = 'dinner-mask-main dinner-background-mask']//div[@class = 'center']//div[@aria-label = 'customizedAlert']//div[@class = 'alert-customize-main-button']//span[@aria-label='buttonOkay']//div[text()='Yes']";
	protected static final String DELETE_PHOTO_TODAYS_IMG_LABEL3 = "//div[@class='dinner-delete-photo-todays-img-label3']";
	protected static final String UPLOAD_FOTO_TODAYS_ADMIN = "//div[@class='dinner-button-upload-foto-todays-admin']";
	protected static final String SAVE_TODAYS_ADMIN = "//div[@class='dinner-button-save-todays-admin']";
	protected static final String INPUT_DELIVERED_ADMIN = "//input[@class='dinner-input-delivered-admin']";
	protected static final String INPUT_PREPARED_ADMIN = "//input[@class='dinner-input-prepared-admin']";
	protected static final String INPUT_ORDER_ADMIN = "//input[@class='dinner-input-order-admin']";
	protected static final String DESCRIPTIONS = "//textarea[@placeholder='Write Details & Descriptions']";
	protected static final String NAME = "//input[@placeholder='Write Menu Name']";
	protected static final String TodaysMenu_Button = "//div[@class='dinner-button-label-todaysmenu']";
	String mainContent = "//div[@class = 'dinner-main-content-masking']//div[@class = 'dinner-mask-main dinner-background-mask']//div[@class = 'center']";
	String popUpAlert = "//div[@aria-label = 'customizedAlert']//div[@class = 'alert-customize-main-button']//span[@aria-label='buttonOkay']";
	String buttonOk = "//div[text()='OK']";
	String okButtonOnPopup = mainContent + popUpAlert + buttonOk ;
	
	
	public int waitForText (int a) {
		for (int second = 0;; second++) {
			if (second >= 60);
			try {if (second >= a -1) break;} catch(Exception e){}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return a;
	}
	public void waitForElement(DefaultSelenium selenium,String element) throws Exception {
		System.out.print("Wait...");
		while (!selenium.isTextPresent(element)){
			waitForText(2);
			System.out.print(".");
			try {if (selenium.isVisible(element))
			System.out.println("This element Visible : "+element);
			break;} catch (Exception e){}
			try {Thread.sleep(1000);}
			catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void waitSecond(WebDriver browser, String element) {
		WebElement checkThisElement = browser.findElement(By.xpath(element)); 
		while (!checkThisElement.isDisplayed()){
			waitForText(5);
			System.out.println(WAIT_FOR_ELEMENT_VISIBLE);
//			try {if (checkThisElement.isDisplayed())
//			System.out.println("This element has display: "+element);
//			break;
//			}catch (Exception e){}
		}
	}
	/* To wait Element Until Visible */
	public static WebElement explicitWait(WebDriver browser, final By by){
		Wait<WebDriver> wait = new FluentWait<WebDriver>(browser)
				.withTimeout(30, TimeUnit.SECONDS)
				.pollingEvery(5, TimeUnit.SECONDS)
				.ignoring(NoSuchElementException.class);
		WebElement element = wait.until(new Function<WebDriver, WebElement>() {
			@Override
			public WebElement apply(WebDriver browser) {
                return browser.findElement(by);
        }
		});
		return element;}

}
